package com.crunch.core;

public class Transform2D {
	private float[] matrix = {
			1.0f, 0.0f, 0.0f,
			0.0f, 1.0f, 0.0f,
			0.0f, 0.0f, 1.0f
	};

	private float tx, ty;
	private float sx, sy;
	private float rot;

	public float getTranslationX() { return tx; }
	public float getTranslationY() { return ty; }
	public float getScaleX() { return sx; }
	public float getScaleY() { return sy; }
	public float getRotation() { return rot; }

	// do not modify from this method - make a copy
	public float[] getMatrix() {
		return matrix;
	}

	public float[] getMatrix44() {
		float[] mat44 = {
				matrix[0], matrix[1], 0.0f, matrix[2],
				matrix[3], matrix[4], 0.0f, matrix[5],
				0.0f,      0.0f,      1.0f, 0.0f,
				0.0f,      0.0f,      0.0f, 1.0f
		};
		return mat44;
	}

	public Transform2D() {
		tx = ty = 0.0f;
		sx = sy = 1.0f;
		rot = 0.0f;
	}

	public Transform2D(float tx, float ty) {
		this.tx = tx;
		this.ty = ty;
		sx = sy = 1.0f;
		rot = 0.0f;
		updateMatrix();
	}

	public Transform2D(float tx, float ty, float rot) {
		this.tx = tx;
		this.ty = ty;
		sx = sy = 1.0f;
		this.rot = rot;
		updateMatrix();
	}

	public void setTranslation(float tx, float ty) {
		this.tx = tx;
		this.ty = ty;
		updateMatrix();
	}

	public void setScale(float sx, float sy) {
		this.sx = sx;
		this.sy = sy;
		updateMatrix();
	}

	public void setRotation(float rot) {
		this.rot = rot;
		updateMatrix();
	}

	public void updateMatrix() {
		// optimize for the common case
		if (rot == 0.0f) {
			matrix[0] = sx;   matrix[1] = 0.0f; matrix[2] = tx;
			matrix[3] = 0.0f; matrix[4] = sy;   matrix[5] = ty;
		} else {
			float c = (float) Math.cos(rot);
			float s = (float) Math.sin(rot);
			matrix[0] = c*sx; matrix[1] = -s*sy; matrix[2] = tx;
			matrix[3] = s*sx; matrix[4] = c*sy;  matrix[5] = ty;
		}
	}
}